package com.qf.controller;

import com.qf.common.http.Result;
import com.qf.entiy.dto.Order;
import com.qf.entiy.po.LoginMerchant;
import com.qf.service.impl.GoodsService;
import com.qf.service.impl.GoodsSkuService;
import com.qf.service.impl.OrderServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderServcie orderServcie;
    @Autowired
    private GoodsSkuService goodsSkuService;

    @RequestMapping("new")
    public String toNew(Model model, @RequestParam(value = "id",required = true)Long goodsId) {
        model.addAttribute("goods",goodsService.getById(goodsId));
        model.addAttribute("buyerNum",orderServcie.getBuyerNumByGoodsId(goodsId));
        model.addAttribute("skuList",goodsSkuService.getSkuListByGoodsId(goodsId));
        return "order/new";
    }


    @RequestMapping("createOrder")
    @ResponseBody
    public Result<?> createOrder(Order order, LoginMerchant loginMerchant){
        orderServcie.createOrder(order,loginMerchant);
        return Result.success();
    }

    @RequestMapping("list")
    public String toList(Model model,LoginMerchant loginMerchant){
        model.addAttribute("orderVoList",orderServcie.getOrderlistById(loginMerchant.getId()));
        return "order/list";
    }

}
